﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Quartz;
using ZX.DBModule;
using ZX.BLL;
using System.Reflection;

namespace ZX.Quartz
{
    public class RunIssueTask :IJob
    {
        public void Execute(IJobExecutionContext context)
        {
            try
            {
                BLL.Extend.BaseBussnies bll = new BLL.Extend.BaseBussnies();
                bll.entity = new SysTask();
                IList<SysTask> list = bll.GetEntitys<SysTask>("RunFlag='0'");
                if (list != null)
                {
                    foreach (var task in list)
                    {
                        string[] _type = task.JobType.Split(',');
                        if (_type.Length > 1)
                        {
                            try
                            {
                                string AssemblyName = _type[1];
                                string TypeName = _type[0];
                                Assembly assem = Assembly.Load(AssemblyName);
                                Type t = assem.GetType(TypeName);
                                SysScheduler.Instance.Insert(task, t);
                                task.RunFlag = "1";

                                bll.entity = task;
                                bll.Update();
                            }
                            catch (System.NullReferenceException e)
                            {
                            }
                            //catch(System.
                        }
                    }
                }
            }
            catch { }
            System.IO.File.Create("D:\\" + System.DateTime.Now.ToString("yyyyMMddmmss") + ".txt");
        }

        public static void Run()
        {
            try
            {
                SysScheduler.Instance.Start();
            }
            catch{}
            try
            {
                SysTask task = new SysTask();
                task.GroupName = "Start";
                task.JobName = "StartTask";
                task.TaskName = "StartTask";
                task.TriggerName = "StartTrriger";
                task.CronExp = "30 1/2 * ? * *";
                task.RunFlag = "1";
                task.JobType = "ZX.Quartz.RunIssueTask,ZX.Quartz";
                task.JobStartDate = DateTime.UtcNow;

                string[] _type = task.JobType.Split(',');
                if (_type.Length > 1)
                {
                    string AssemblyName = _type[1];
                    string TypeName = _type[0];
                    Assembly assem = Assembly.Load(AssemblyName);
                    Type t = assem.GetType(TypeName);
                    SysScheduler.Instance.Insert(task, t);
                }

            }
            catch { }
        }
    }
}
